(function($){
	$(window).load(function(){
		/************登录*************/
		var userName=getCookie('username');
		if(userName!=''){
			$('#login').html('欢迎您！ '+userName+' <a href="javascript:;"> [ 退出  ] </a>');
			$('#login a').click(function(){
				$('#login').html('嘿，欢迎来顺丰优选！<a href="login.html">请登录 </a><a href="reg.html"> 免费注册</a>')
			});
		}
		$('.menu').mouseenter(function(){
			$('.main_nav').css('display','block');
		}).mouseleave(function(){
			$('.main_nav').css('display','none');
		});
		$('.big_menu').mouseenter(function(){
			$('.main_nav').css('display','block');
		}).mouseleave(function(){
			$('.main_nav').css('display','none');
		});
		var 
			oSpan=$('.small_pic span'),
			oSmall=$('.small_pic'),
			oBig=$('.big_pic'),
			oSmallImg=$('.small_pic img'),
			oBigImg=$('.big_pic img'),
			oSpanW=oSpan.width(),
			oSpanH=oSpan.height(),
			oSmallImgW=oSmallImg.width(),
			oSmallImgH=oSmallImg.height(),
			scrollT=0,
			scrollL=0;
		$(window).scroll(function(){
			scrollT=$('body').scrollTop();
			scrollL=$('body').scrollLeft();
		});
		oSmall.mouseover(function(){
			oSpan.css('display','block');
			oBig.css('display','block');
		}).mouseout(function(){
			oSpan.css('display','none');
			oBig.css('display','none');
		}).mousemove(function(e){
			var 
				e=e||event,
				eX=e.clientX,
				eY=e.clientY,
				spanLeft=eX-oSmallImg.offset().left-oSpan.width()/2+scrollL,
				spanTop=eY-oSmallImg.offset().top-oSpan.height()/2+scrollT;
			if(spanLeft<=0){
				spanLeft=0;
			}else if(spanLeft>oSmallImgW-oSpanW){
				spanLeft=oSmallImgW-oSpanW;
			}
			if(spanTop<=0){
				spanTop=0;
			}else if(spanTop>oSmallImgH-oSpanH){
				spanTop=oSmallImgH-oSpanH;
			}
			oSpan.css({left:spanLeft,top:spanTop});
			var 
				perX=spanLeft/(oSmallImgW-oSpanW),
				perY=spanTop/(oSmallImgH-oSpanH),
				oBigW=oBig.width(),
				oBigH=oBig.height(),
				oBigImgW=oBigImg.width(),
				oBigImgH=oBigImg.height(),
				oBigLeft=perX*(oBigW-oBigImgW),
				oBigTop=perY*(oBigH-oBigImgH);	
			oBigImg.css({
				left:oBigLeft,
				top:oBigTop
			});
		});
		var picUlT=parseInt($('.pic_list ul').css('top'));
		$('.pic_pre').click(function(){
			var picUlT=parseInt($('.pic_list ul').css('top'));
			console.log(picUlT)
			if(picUlT>=-180&&picUlT<0){
				picUlT+=60;
			}else{
				return false;
			}
			$('.xiang_lunbo ul').css('top',picUlT);
			console.log($('.pic_list ul').css('top'));
		});
		$('.pic_next').click(function(){
			var picUlT=parseInt($('.pic_list ul').css('top'));
			console.log(picUlT)
			if(picUlT>=-120){
				picUlT-=60;
			}else{
				return false;return false;
			}
			$('.xiang_lunbo ul').css('top',picUlT);
		});
		$('.xiang_lunbo ul li').each(function(i){
			$(this).mouseover(function(){
				$('.small_pic img').attr('src','../images/xiangxi_pic0'+(i+1)+'.jpg');
				$('.big_pic img').attr('src','../images/big0'+(i+1)+'.jpg');
			});
			
		});
		/*************************************地址***********************************/
		var 
			divSheng=$('.dis_city ul .add_sheng div'),
			divShi=$('.dis_city ul .add_shi div ');
		$('.dis_address').mouseenter(function(){
			$('.dis_city').addClass('hover');
			divShi.css('display','none');
			divSheng.css('display','block');
			$('.add_sheng').css({
				borderBottom:0,
			});
		}).mouseleave(function(){
			$('.dis_city').removeClass('hover');
		});
		
		$('.li_close').click(function(){
			$('.dis_city').removeClass('hover');
		});
		$('.dis_city li').each(function(i){
			if(i<2){
				$('.dis_city li').eq(i).click(function(){
					$('.dis_city li').css('border-bottom','2px solid #e8e8e8').eq(3).css('border-bottom','0');
					$(this).css('border-bottom','0');
					$('.dis_city li div').css('display','none').eq(i).css('display','block');
				})
			}else{
				return false;
			}
		});
		var shengName=$('.add_sheng div p a');
		var shiName=$('.add_shi div p a');
		shengName.each(function(i){
			$(this).click(function(){
				var text=$('.add_sheng div p a').eq(i).html();
				$('.dis_address span').eq(0).html(text);
				$('.add_sheng span').html(text);
				
			});
		})
		shiName.each(function(i){
			$(this).click(function(){
				var text=$('.add_shi div p a').eq(i).html();
				$('.add_shi span').html(text);
				$('.dis_address span').eq(0).text($('.dis_address span').eq(0).text()+text);
			});
		});
		
		/*************类型************/
		
		$('.pro_type span').each(function(i){
			$(this).click(function(){
				$('.pro_type span').removeClass('pro_clicktype').eq(i).addClass('pro_clicktype');
			})
		});
		/**************数量**************/
		var num=parseInt($('.shuliang input').val());
		$('.pro_jia').click(function(){
			num++;
			$('.shuliang input').val(num);
		})
		$('.pro_jian').click(function(){
			num--;
			if(num<0){
				num=0;
			}
			$('.shuliang input').val(num);
		});
		
		/*************吸顶**********************************/
		var scrollT=$('html,body').scrollTop();
		$(window).scroll(function(){
			var scrollT=$('html,body').scrollTop();
			if(scrollT>=745){
				$('.pro_menu').addClass('xiding')
			}else{
				$('.pro_menu').removeClass('xiding')
			}
			
		})
		$('.pro_menu a').each(function(i){
			$(this).click(function(){
				$('.pro_menu a').removeClass('aclick').eq(i).addClass('aclick');
				if(i==2||i==1){
					$('html,body').animate({
						scrollTop:8300
					},1000);
				}else if(i==0){
					$('html,body').animate({
						scrollTop:745
					},1000);
				}
			})
		});
		
		
		$('.pro_pl_menu a').each(function(i){
			
			$(this).mouseover(function(){
				$('.pro_pl_menu a').removeClass('pro_pl_hover').eq(i).addClass('pro_pl_hover');
			})
		})
		var iIndex=0;
		var count=0;
		var timer=setInterval(move,1000);
		function move(){
			clearInterval(timer);
			count+=780;
			$('.pro_lunbo_box ul').stop(true).delay(500).animate({left:-count},500,function(){
				if(iIndex>=5){
					iIndex=-1;
				}
				$('.pro_btn a').removeClass('pro_btn_hover').eq(++iIndex).addClass('pro_btn_hover');
				clear();
			});
		}
		function clear(){
			timer=setInterval(move,3000);
			if(count>=3900){
				count=0;
				$('.pro_lunbo_box ul').css('left','0');
			}
		}
		$('.pro_lunbo_box').mouseenter(function(){
			clearInterval(timer);
			$('.pro_lunbo_box ul').stop(true);
		}).mouseleave(function(){
			move();
		});
		/**********************加入购物车******************/
		function setCookie(){
			var goodMess=getCookie('mess');
			if(goodMess==''||goodMess==null){
				var obj=new Object();
				obj.id=8;
				obj.name=escape($('.pro_clicktype').text());
				obj.src=$('.small_pic img').attr('src');
				obj.price=$('.pro_price strong').text();
				obj.num=parseInt($('.shuliang input').val());
				var arr=[obj];
				document.cookie="mess="+JSON.stringify(arr)+";path=/";
			}else{
				var arr=JSON.parse(goodMess);
				var obj=new Object();
				var bFlag=true;
				obj.id=8;
				obj.name=escape($('.pro_clicktype').text());
				obj.src=$('.small_pic img').attr('src');
				obj.price=$('.pro_price strong').text();
				obj.num=parseInt($('.shuliang input').val());
				for(var j=0;j<arr.length;j++){
					if(arr[j].id==8){
						bFlag=false;
						arr[j].num=parseInt(arr[j].num)+obj.num;
					}
				}
				if(bFlag){
					arr.push(obj);
				}
			}
			document.cookie="mess="+JSON.stringify(arr)+";path=/";
		}
		function getCookie(name){
			var str=document.cookie;
			var arr=str.split("; ");
			for(var i=0;i<arr.length;i++){
				var address=arr[i].indexOf("=");
				var name1=arr[i].slice(0,address);
				var value=arr[i].slice(address+1,arr[i].length);
				if(name==name1){
					return value;
				}
			}
			return "";
		}	
		$('.pro_car').click(function(){
			setCookie();
			if(getCookie('mess')==''||getCookie('mess')==null){
				return false;
			}else{
				if(confirm('去购物车结算？')){
					window.open('car.html');
				}else{
					return false;
				}
			}
			
		});
		$('.aputcar').click(function(){
			setCookie();
		});
		/***********购物车链接************/
		$('.slide_shop').click(function(){
			window.open('car.html');
		});
		/**********购物车数量**********/
		var goodMess=getCookie('mess');
		if(goodMess==''||goodMess==null){
			$('.hover02').text(0);
		}else{
			var arr=JSON.parse(goodMess);
			$('.hover02').text(arr.length);
			if($('.hover02').text()!='0'){
				$('.shop_car').click(function(){
					window.open('car.html');
				});
			}
		}
		$('.slide_common').eq(0).mouseover(function(){
			var goodMess=getCookie('mess');
			if(goodMess==''||goodMess==null){
				$('.shop_car_list').css('display','block');
			}else{
				$('.shop_car_list').css('display','none');
			}
		});
		$('.slide_common').eq(0).click(function(){
			var goodMess=getCookie('mess');
			if(goodMess==''||goodMess==null){
				$('.shop_car_list').css('display','block');
			}else{
				window.open('car.html');
			}
		});
	});
})(jQuery);
